import Vue from 'vue'
import VueRouter from "vue-router"
import { Message } from 'element-ui'

import store from '../store/store'

Vue.use(VueRouter);

import Home from '../pages/Home/Home.vue'
import Login from '../pages/Login/Login.vue'
import Search from '../pages/Search/Search.vue'
import MyOrder from '../pages/MyOrder/MyOrder.vue'
import Register from '../pages/Register/Register.vue'
import GoodsDetail from '../pages/GoodsDetail/GoodsDetail.vue'
import AddGoods_Succe from '../pages/AddGoods_Succe/AddGoods_Succe.vue'
import shopCart from '../pages/shopCart/shopCart.vue'
import TradeInfo from '../pages/TradeInfo/TradeInfo.vue'
import Pay from '../pages/Pay/Pay.vue'
import PaySuccess from '../pages/PaySuccess/PaySuccess.vue'

// 二级路由
import MyOrder_1 from '../pages/MyOrder/MyOrder_1/MyOrder_1.vue'
import MyOrder_2 from '../pages/MyOrder/MyOrder_2/MyOrder_2.vue'

// .... 重写 push 和 replace
const my_push = VueRouter.prototype.push
VueRouter.prototype.push = function (path, successCallback, errorCallback) {
    if (successCallback && errorCallback) {
        my_push.call(this, path, successCallback, errorCallback)
    }else if (successCallback) {
        my_push.call(this, path, successCallback, () => { })
    } else {
        my_push.call(this, path, () => { }, () => { })
    }
}
const my_replace = VueRouter.prototype.replace
VueRouter.prototype.replace = function (path, successCallback, errorCallback) {
    if (successCallback && errorCallback) {
        my_push.call(this, path, successCallback, errorCallback)
    }else if (successCallback) {
        my_push.call(this, path, successCallback, () => { })
    } else {
        my_push.call(this, path, () => { }, () => { })
    }
}


const router =  new VueRouter({
    routes: [
        {path: '/', redirect: '/home'},
        {path: '/home', component: Home},
        {path: '/login', component: Login, meta: {isShowFlooter: 'hide'}},
        {path: '/search:keyword?', component: Search, name: "search" },
        {
            path: '/myorder', component: MyOrder, redirect: 'myorder/myorder_1', children: [
                {path: '/myorder/myorder_1', component: MyOrder_1},
                {path: '/myorder/myorder_2', component: MyOrder_2}
        ]},
        {path: '/detail', component: GoodsDetail},
        {path: '/register', component: Register, meta: {isShowFlooter: 'hide'}},
        {
            path: '/add_goods_succ', component: AddGoods_Succe,
            beforeEnter(to, from, next) {
                if (from.fullPath.indexOf('/detail?skuId') != -1 && to.query.isclick) {
                    next()
                }else {
                    Message.error('哒买油!')
                    next(false)
                }
            }
        },
        {path: '/shopcart', component: shopCart},
        {path: '/tradeinfo', component: TradeInfo},
        {path: '/pay', component: Pay},
        {path: '/paysuccess', component: PaySuccess},
    ],
    scrollBehavior (to, from, savedPosition) {
        // return 期望滚动到哪个的位置
        return {y: 0}
    }
});

// 定义全局前置路由守卫
router.beforeEach(async (to, from, next) => {
    if (store.state.login.token) {
        if (to.path === '/login') {
            next('/home')
        } else {
            if (store.state.login.UserInfo.name) {
                next()
            } else {
                try {
                    await store.dispatch('login/get_userInfo')
                    next()
                } catch (error) {
                    // 代表token过期
                    // 掉推出登录
                    store.dispatch('login/clear_token_userinfo')
                    next()
                }
            }
        }
    } else {
        if (to.path.indexOf('order') != -1 || to.path.indexOf('pay') != -1 || to.path.indexOf('tradeinfo') != -1) {
            Message.error('请登录!')
            return next(false)
        }
        next()
    }
});

export default router